$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/courseuser/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			// { label: '用户id', name: 'userid', index: 'userid', width: 80 },
            { label: '用户', name: 'userid', index: 'userid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var username = "" ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/users/info/"+rowdata.userid,
                        success: function(r){
                            username = r.users.username;

                        }
                    });
                    return username;
                }
            },
			// { label: '课程id', name: 'courseid', index: 'courseid', width: 80 },
            { label: '课程', name: 'courseid', index: 'courseid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var username ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/course/info/"+rowdata.courseid,
                        success: function(r){
                            username = r.course.title;

                        }
                    });
                    return username;
                }
            },
			{ label: '报名时间', name: 'createtime', index: 'createtime', width: 80 }, 			
			// { label: '状态 0 报名中 1 已报名 2 已结束', name: 'statu', index: 'statu', width: 80 },
            { label: '状态', name: 'statu', width: 50, formatter: function(value, options, row){
                    if (value ==0) {
                        return '<span class="label label-danger">报名中</span>' ;
                    }  else if (value ==1) {
                        return '<span class="label label-success">已报名</span>' ;
                    }  else if (value ==2) {
                        return '<span class="label label-success">已结束</span>' ;
                    }else if (value ==3) {
                        return '<span class="label label-danger">未通过</span>' ;
                    }
                }},
			// { label: '题目解析', name: 'topicanalysis', index: 'topicanalysis', width: 80 },
			// { label: '答题时间', name: 'answertime', index: 'answertime', width: 80 },
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                if(rowdata.statu==1||rowdata.statu==2||rowdata.statu==3){
                    return "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>"+
                        "<span class=\"label label-danger\">删除</span></a>";
                }else{
                    return" <a style='cursor:pointer;' onclick='vm.pass("+ rowdata.id +")'>" +
                        "<span class=\"label label-danger\">通过</span></a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.nopass("+ rowdata.id +")'>"+
                        "<span class=\"label label-danger\">未通过</span></a>&nbsp;&nbsp;&nbsp;&nbsp;"+
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>"+
                        "<span class=\"label label-danger\">删除</span></a>";
                }

                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            statu: null
        },
		showList: true,
		title: null,
        courses: {},
        users: {},
		courseuser: {
            username: null,
            coursename: null
        }
	},
    courses: {},
    users: {},
    courseuser: {
        username: null,
        coursename: null
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            vm.getCourses();
            vm.getUsers();
			vm.showList = false;
			vm.title = "新增";
			vm.courseuser = {
                username: null,
                userid: null,
                coursename: null,
                courseid: null
            };
		},
		update: function (event) {
            vm.getCourses();
            vm.getUsers();
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                vm.courseuser.createtime = $("#createtime").val()
                var url = vm.courseuser.id == null ? "sys/courseuser/save" : "sys/courseuser/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.courseuser),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (id) {
			// var ids = getSelectedRows();
			// if(ids == null){
			// 	return ;
			// }
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/courseuser/deleteById",
                        contentType: "application/json",
                        data: JSON.stringify(id),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        pass: function (id) {
            // var ids = getSelectedRows();
            // if (ids == null) {
            //     return;
            // }
            var lock = true;
            layer.confirm('确认通过？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/courseuser/pass",
                        contentType: "application/json",
                        data: JSON.stringify(id),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        nopass: function (id) {
            // var ids = getSelectedRows();
            // if (ids == null) {
            //     return;
            // }
            var lock = true;
            layer.confirm('确定取消通过？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/courseuser/nopass",
                        contentType: "application/json",
                        data: JSON.stringify(id),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
		getInfo: function(id){
			$.get(baseURL + "sys/courseuser/info/"+id, function(r){
                vm.courseuser = r.courseuser;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {'statu': vm.q.statu
                },
                page:page
            }).trigger("reloadGrid");
		},
        getCourses: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/course/getCourses",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.courses = r.data;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setCourseTitle: function (id) {
            for (let i = 0; i < vm.courses.length; i++) {
                if (id == vm.courses[i].id) {
                    vm.courseuser.coursename = vm.courses[i].title;
                }
            }
            console.log(vm);
        },
        getUsers: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/users/getUsers0",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.users = r.data;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setUsername: function (id) {
            for (let i = 0; i < vm.users.length; i++) {
                if (id == vm.users[i].id) {
                    vm.courseuser.username = vm.users[i].username;
                }
            }
            console.log(vm);
        },
	}
});